<?
include_once("header_inc.php");

$score = 0;
$cur_grade = 0;
$user_id=getParamInt("user_id","");


$r = q($sql = "select count(ad_id) from $ads_tbl where ad_owner=$user_id");
$row_user = mysql_fetch_array($r);
$number = $row_user["count(ad_id)"];

$r = q($sql = "select count(ad_id) from $ads_tbl where ad_owner=$user_id AND ad_is_sold=1");
$row_user = mysql_fetch_array($r);
$number_sold = $row_user["count(ad_id)"];

$r = q($sql = "select sum(ad_views) from $ads_tbl where ad_owner=$user_id");
$row_user = mysql_fetch_array($r);
$number_hits = $row_user["sum(ad_views)"];

$r = q($sql = "select sum(ad_votes) from $ads_tbl where ad_owner=$user_id");
$row_user = mysql_fetch_array($r);
$number_ad_votes = $row_user["sum(ad_votes)"];

$r = q($sql = "select sum(ad_voters) from $ads_tbl where ad_owner=$user_id");
$row_user = mysql_fetch_array($r);
$number_ad_voters = $row_user["sum(ad_voters)"];

if ($number_ad_voters>0)
	$score = $number_ad_votes/$number_ad_voters;

$r = q($sql = "select * from $usr_tbl where user_id = $user_id");
$row_user = mysql_fetch_array($r);
$name = $row_user["name"];
$is_vendor = $row_user["user_is_vendor"];
$vendor_url = $row_user["user_vendor_url"];
$vendor_homepage = $row_user["user_vendor_homepage"];
$votes = $row_user["user_votes"];
$voters = $row_user["user_voters"];
$name = $row_user["user_name"];
if ($voters > 0)
	$cur_grade = round($votes / $voters, 1);

$tplList=new TplLoad();
$tplList->assign("user_id",$user_id);
$tplList->assign("ad_id",getParam("id",""));

// Comment system
if (($_SESSION["valid_user"] !="") && (getParam("submit","") !="") && (getParam("comment_body","") !=""))
{
	$lfBody=htmlspecialchars(getParam("comment_body",""),ENT_QUOTES);
	$lfUser=$_SESSION["valid_user"];
	$lSql="insert into $com_tbl (comment_body,comment_date,comment_userid,comment_user,comment_ip) values ('$lfBody',".time().",$lfUser,$user_id,'".getRemoteIp()."')";
	$lCommentRes=q($lSql);
	$lCommentDone=1;
	
}
if (getParam("del","") && $set_comment_admin_del)
{
	if ($_SESSION["valid_user"]==$user_id)
	{
		q("delete from $com_tbl where comment_id=" . getParamInt("del","") . " AND comment_user=$user_id");
	}
}
$lSql="select * from $com_tbl where comment_user=" . $user_id . " order by comment_id desc";
$lComment=q($lSql);
$i=0;
while ($lRow=mysql_fetch_array($lComment))
{
	$lComments[$i]["comment_body"]=$lRow["comment_body"];
	$lComments[$i]["comment_id"]=$lRow["comment_id"];
	$lComments[$i]["comment_userid"]=$lRow["comment_userid"];
	$lComments[$i]["comment_date"]=formatDateShort($lRow["comment_date"]);
	$lSq="select user_name from $usr_tbl where user_id=" . $lRow["comment_userid"];
	$lResult=q($lSq);
	$lRow2=mysql_fetch_array($lResult);
	$lComments[$i]["comment_by"]=$lRow2["user_name"];
	
	$i++;

}
$tplList->assign("comment_list",$lComments);
// END comment system


$tplList->assign("isLoggedIn",$_SESSION["valid_user"]);
$tplList->assign("set_comment_system",$set_comment_system);
$tplList->assign("NUMBER_OF_ADS",$number);
$tplList->assign("NUMBER_OF_ADS_SOLD",$number_sold);
$tplList->assign("NUMBER_OF_ADVIEWS",$number_hits);
$tplList->assign("CURRENT_GRADE",$cur_grade);
$tplList->assign("CURRENT_GRADE_ADS",$score);
$tplList->assign("DESCRIPTION",$title);
$tplList->assign("NAME",$name);
$tplList->assign("USER_ID",$user_id);
$tplList->assign("user_id",$user_id);
$tplList->assign("IS_VENDOR",$is_vendor);
$tplList->assign("VENDOR_URL",$vendor_url);
$tplList->assign("VENDOR_HOMEPAGE",$vendor_homepage);
if ($lCommentDone==1)
	$tplList->assign("set_comment_done",1);

// Extra User fields
 $fExtraArray=getFieldArray("",$user_id,",");
 $lUserArrayExtra=$fExtraArray["user"][$user_id];
 if (is_array($lUserArrayExtra))
 {
	foreach ($lUserArrayExtra as $lKey => $lValue) 
	{
		if ($lValue["question"])
			$lUserArrayExtra[$lKey]["question"]=(constant($lValue["question"]));
		if ($lValue["description"])
			$lUserArrayExtra[$lKey]["description"]=(constant($lValue["description"]));
	}
	
 }
 
 $tplList->assign("user_extra",$lUserArrayExtra);

// Member Rating
if ($set_vote_on_member)
		$tplList->assign("rate_member",$set_vote_on_member);
if ($set_ratemem_login_need)
		$tplList->assign("rate_member_requires_login","$set_ratemem_login_need");
if ($voters > 0)
	$votes = round($votes/$voters, 1);
else {
	$votes=0;
	$voters=0;
}
$tplList->assign("ad_member_votes","$votes");
$tplList->assign("ad_member_voters","$voters");

if (getParam("contact",""))
	include "contact.php";


$useritems=1;
require("links.php");
include_once("footer_inc.php");
?>